namespace PMS.BLL
{
    using PMS.Common;
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganImportBLL
    {
        private CommandInfo GetOrganCommandInfo(int parentID, Organ model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Organ(");
            builder.Append("OrganLevelID,OrganTypeID,OrganBuildID,OrganDescription,OrganBranchCode,PermitNo,Functions,BankName,BankID,LegalPerson,OrganName,PermitOrganCount,RealityOrganCount,Address,Postcode,Contact,Tel,Remark,AddDate,AddUser,UpdateDate,OrganName2,UpdateUser,PayWay,IsOffice,LevelNo,ParentID,OrganCode,OrganClassID,OrganRoleID");
            builder.Append(") values (");
            builder.Append("@OrganLevelID,@OrganTypeID,@OrganBuildID,@OrganDescription,@OrganBranchCode,@PermitNo,@Functions,@BankName,@BankID,@LegalPerson,@OrganName,@PermitOrganCount,@RealityOrganCount,@Address,@Postcode,@Contact,@Tel,@Remark,@AddDate,@AddUser,@UpdateDate,@OrganName2,@UpdateUser,@PayWay,@IsOffice,@LevelNo,@ParentID,@OrganCode,@OrganClassID,@OrganRoleID");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] parameterArray = new SqlParameter[] { 
                new SqlParameter("@OrganLevelID", model.OrganLevelID), new SqlParameter("@OrganTypeID", model.OrganTypeID), new SqlParameter("@OrganBuildID", model.OrganBuildID), new SqlParameter("@OrganDescription", model.OrganDescription), new SqlParameter("@OrganBranchCode", model.OrganBranchCode), new SqlParameter("@PermitNo", model.PermitNo), new SqlParameter("@Functions", model.Functions), new SqlParameter("@BankName", model.BankName), new SqlParameter("@BankID", model.BankID), new SqlParameter("@LegalPerson", model.LegalPerson), new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@PermitOrganCount", model.PermitOrganCount), new SqlParameter("@RealityOrganCount", model.RealityOrganCount), new SqlParameter("@Address", model.Address), new SqlParameter("@Postcode", model.Postcode), new SqlParameter("@Contact", model.Contact), 
                new SqlParameter("@Tel", model.Tel), new SqlParameter("@Remark", model.Remark), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@UpdateDate", model.UpdateDate), new SqlParameter("@OrganName2", model.OrganName2), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@PayWay", model.PayWay), new SqlParameter("@IsOffice", model.IsOffice), new SqlParameter("@LevelNo", model.LevelNo), new SqlParameter("@ParentID", parentID), new SqlParameter("@OrganCode", model.OrganCode), new SqlParameter("@OrganClassID", model.OrganClassID), new SqlParameter("@OrganRoleID", model.OrganRoleID)
             };
            return new CommandInfo { CommandText = builder.ToString(), Parameters = parameterArray };
        }

        private CommandInfo GetOrganLogCommandInfo(int organID, OrganChangeLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganChangeLog(");
            builder.Append("OrganClassID,OrganRoleID,OrganLevelID,OrganTypeID,OrganBuildID,OrganDescription,OrganBranchCode,PermitNo,Functions,BankName,OrganID,BankID,LegalPerson,PermitOrganCount,RealityOrganCount,Address,Postcode,Contact,Tel,Remark,AddDate,OrganName,AddUser,UpdateDate,UpdateUser,LogType,CheckedState,CheckDate,CheckAdminID,CheckRemark,OrganName2,PayWay,IsOffice,LevelNo,ParentID,ParentName,OrganCode");
            builder.Append(") values (");
            builder.Append("@OrganClassID,@OrganRoleID,@OrganLevelID,@OrganTypeID,@OrganBuildID,@OrganDescription,@OrganBranchCode,@PermitNo,@Functions,@BankName,@OrganID,@BankID,@LegalPerson,@PermitOrganCount,@RealityOrganCount,@Address,@Postcode,@Contact,@Tel,@Remark,@AddDate,@OrganName,@AddUser,@UpdateDate,@UpdateUser,@LogType,@CheckedState,@CheckDate,@CheckAdminID,@CheckRemark,@OrganName2,@PayWay,@IsOffice,@LevelNo,@ParentID,@ParentName,@OrganCode");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] parameterArray = new SqlParameter[] { 
                new SqlParameter("@OrganClassID", model.OrganClassID), new SqlParameter("@OrganRoleID", model.OrganRoleID), new SqlParameter("@OrganLevelID", model.OrganLevelID), new SqlParameter("@OrganTypeID", model.OrganTypeID), new SqlParameter("@OrganBuildID", model.OrganBuildID), new SqlParameter("@OrganDescription", model.OrganDescription), new SqlParameter("@OrganBranchCode", model.OrganBranchCode), new SqlParameter("@PermitNo", model.PermitNo), new SqlParameter("@Functions", model.Functions), new SqlParameter("@BankName", model.BankName), new SqlParameter("@OrganID", organID), new SqlParameter("@BankID", model.BankID), new SqlParameter("@LegalPerson", model.LegalPerson), new SqlParameter("@PermitOrganCount", model.PermitOrganCount), new SqlParameter("@RealityOrganCount", model.RealityOrganCount), new SqlParameter("@Address", model.Address), 
                new SqlParameter("@Postcode", model.Postcode), new SqlParameter("@Contact", model.Contact), new SqlParameter("@Tel", model.Tel), new SqlParameter("@Remark", model.Remark), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@UpdateDate", model.UpdateDate), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@LogType", model.LogType), new SqlParameter("@CheckedState", model.CheckedState), new SqlParameter("@CheckDate", model.CheckDate), new SqlParameter("@CheckAdminID", model.CheckAdminID), new SqlParameter("@CheckRemark", model.CheckRemark), new SqlParameter("@OrganName2", model.OrganName2), new SqlParameter("@PayWay", model.PayWay), 
                new SqlParameter("@IsOffice", model.IsOffice), new SqlParameter("@LevelNo", model.LevelNo), new SqlParameter("@ParentID", model.ParentID), new SqlParameter("@ParentName", model.ParentName), new SqlParameter("@OrganCode", model.OrganCode)
             };
            return new CommandInfo { CommandText = builder.ToString(), Parameters = parameterArray };
        }

        private CommandInfo GeUserCommandInfo(int organID, User model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into [User](");
            builder.Append("GraduationDate,GraduationSchoole,HighestEducation,Degree,Major,JoinWorkDate,Photo,IsOfficer,Remark,RegistDay,UserName,RegistWay,RegistFrom,RegistTo,OrganID,RegistNo,RegistDate,RegistSummary,RegistRemark,RegistState,RegistType,UserType,Sex,IsHeighWith,JobPositionType,JobPositionName1,JobPositionDate1,JobPositionName2,JobPositionDate2,JobPositionNo,JobPositionDate,JobPositionSummary,JobPositionRemark,Birthday,AdminPosition,EvaluationDate,Position1,PositionLevel1,JobTitle1,PositionStartDate1,PositionEndDate1,PositionNo1,PositionDate1,PositionRemark1,ID,IsDoublePosition,Position2,PositionLevel2,JobTitle2,PositionStartDate2,PositionEndDate2,PositionNo2,PositionDate2,PositionRemark2,Class,Birthplace,ClassDate,IsLeave,LeaveReasion,LeaveDay,LeaveTo,LeaveNo,LeaveDate,LeaveSummary,LeaveRemark,AddUser,Nation,AddDate,UpdateUser,UpdateDate,Backup1,Backup2,Backup3,Backup4,Backup5,Backup6,Backup7,Politics,JoinPartDate");
            builder.Append(") values (");
            builder.Append("@GraduationDate,@GraduationSchoole,@HighestEducation,@Degree,@Major,@JoinWorkDate,@Photo,@IsOfficer,@Remark,@RegistDay,@UserName,@RegistWay,@RegistFrom,@RegistTo,@OrganID,@RegistNo,@RegistDate,@RegistSummary,@RegistRemark,@RegistState,@RegistType,@UserType,@Sex,@IsHeighWith,@JobPositionType,@JobPositionName1,@JobPositionDate1,@JobPositionName2,@JobPositionDate2,@JobPositionNo,@JobPositionDate,@JobPositionSummary,@JobPositionRemark,@Birthday,@AdminPosition,@EvaluationDate,@Position1,@PositionLevel1,@JobTitle1,@PositionStartDate1,@PositionEndDate1,@PositionNo1,@PositionDate1,@PositionRemark1,@ID,@IsDoublePosition,@Position2,@PositionLevel2,@JobTitle2,@PositionStartDate2,@PositionEndDate2,@PositionNo2,@PositionDate2,@PositionRemark2,@Class,@Birthplace,@ClassDate,@IsLeave,@LeaveReasion,@LeaveDay,@LeaveTo,@LeaveNo,@LeaveDate,@LeaveSummary,@LeaveRemark,@AddUser,@Nation,@AddDate,@UpdateUser,@UpdateDate,@Backup1,@Backup2,@Backup3,@Backup4,@Backup5,@Backup6,@Backup7,@Politics,@JoinPartDate");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] parameterArray = new SqlParameter[] { 
                new SqlParameter("@GraduationDate", model.GraduationDate), new SqlParameter("@GraduationSchoole", model.GraduationSchoole), new SqlParameter("@HighestEducation", model.HighestEducation), new SqlParameter("@Degree", model.Degree), new SqlParameter("@Major", model.Major), new SqlParameter("@JoinWorkDate", model.JoinWorkDate), new SqlParameter("@Photo", model.Photo), new SqlParameter("@IsOfficer", model.IsOfficer), new SqlParameter("@Remark", model.Remark), new SqlParameter("@RegistDay", model.RegistDay), new SqlParameter("@UserName", model.UserName), new SqlParameter("@RegistWay", model.RegistWay), new SqlParameter("@RegistFrom", model.RegistFrom), new SqlParameter("@RegistTo", model.RegistTo), new SqlParameter("@OrganID", organID), new SqlParameter("@RegistNo", model.RegistNo), 
                new SqlParameter("@RegistDate", model.RegistDate), new SqlParameter("@RegistSummary", model.RegistSummary), new SqlParameter("@RegistRemark", model.RegistRemark), new SqlParameter("@RegistState", model.RegistState), new SqlParameter("@RegistType", model.RegistType), new SqlParameter("@UserType", model.UserType), new SqlParameter("@Sex", model.Sex), new SqlParameter("@IsHeighWith", model.IsHeighWith), new SqlParameter("@JobPositionType", model.JobPositionType), new SqlParameter("@JobPositionName1", model.JobPositionName1), new SqlParameter("@JobPositionDate1", model.JobPositionDate1), new SqlParameter("@JobPositionName2", model.JobPositionName2), new SqlParameter("@JobPositionDate2", model.JobPositionDate2), new SqlParameter("@JobPositionNo", model.JobPositionNo), new SqlParameter("@JobPositionDate", model.JobPositionDate), new SqlParameter("@JobPositionSummary", model.JobPositionSummary), 
                new SqlParameter("@JobPositionRemark", model.JobPositionRemark), new SqlParameter("@Birthday", model.Birthday), new SqlParameter("@AdminPosition", model.AdminPosition), new SqlParameter("@EvaluationDate", model.EvaluationDate), new SqlParameter("@Position1", model.Position1), new SqlParameter("@PositionLevel1", model.PositionLevel1), new SqlParameter("@JobTitle1", model.JobTitle1), new SqlParameter("@PositionStartDate1", model.PositionStartDate1), new SqlParameter("@PositionEndDate1", model.PositionEndDate1), new SqlParameter("@PositionNo1", model.PositionNo1), new SqlParameter("@PositionDate1", model.PositionDate1), new SqlParameter("@PositionRemark1", model.PositionRemark1), new SqlParameter("@ID", model.ID), new SqlParameter("@IsDoublePosition", model.IsDoublePosition), new SqlParameter("@Position2", model.Position2), new SqlParameter("@PositionLevel2", model.PositionLevel2), 
                new SqlParameter("@JobTitle2", model.JobTitle2), new SqlParameter("@PositionStartDate2", model.PositionStartDate2), new SqlParameter("@PositionEndDate2", model.PositionEndDate2), new SqlParameter("@PositionNo2", model.PositionNo2), new SqlParameter("@PositionDate2", model.PositionDate2), new SqlParameter("@PositionRemark2", model.PositionRemark2), new SqlParameter("@Class", model.Class), new SqlParameter("@Birthplace", model.Birthplace), new SqlParameter("@ClassDate", model.ClassDate), new SqlParameter("@IsLeave", model.IsLeave), new SqlParameter("@LeaveReasion", model.LeaveReasion), new SqlParameter("@LeaveDay", model.LeaveDay), new SqlParameter("@LeaveTo", model.LeaveTo), new SqlParameter("@LeaveNo", model.LeaveNo), new SqlParameter("@LeaveDate", model.LeaveDate), new SqlParameter("@LeaveSummary", model.LeaveSummary), 
                new SqlParameter("@LeaveRemark", model.LeaveRemark), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@Nation", model.Nation), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@UpdateDate", model.UpdateDate), new SqlParameter("@Backup1", model.Backup1), new SqlParameter("@Backup2", model.Backup2), new SqlParameter("@Backup3", model.Backup3), new SqlParameter("@Backup4", model.Backup4), new SqlParameter("@Backup5", model.Backup5), new SqlParameter("@Backup6", model.Backup6), new SqlParameter("@Backup7", model.Backup7), new SqlParameter("@Politics", model.Politics), new SqlParameter("@JoinPartDate", model.JoinPartDate)
             };
            return new CommandInfo { CommandText = builder.ToString(), Parameters = parameterArray };
        }

        private CommandInfo GeUserLogCommandInfo(int userID, UserChangeLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserChangeLog(");
            builder.Append("Birthday,ID,Birthplace,Nation,Politics,JoinPartDate,GraduationDate,GraduationSchoole,HighestEducation,Degree,LogType,Major,JoinWorkDate,Photo,IsOfficer,Remark,RegistDay,RegistWay,RegistFrom,RegistTo,OrganID,RegistNo,CheckState,RegistDate,RegistSummary,RegistRemark,RegistState,RegistType,UserType,IsHeighWith,JobPositionType,JobPositionName1,JobPositionDate1,CheckAdminID,JobPositionName2,JobPositionDate2,JobPositionNo,JobPositionDate,JobPositionSummary,JobPositionRemark,AdminPosition,EvaluationDate,Position1,PositionLevel1,CheckDate,JobTitle1,PositionStartDate1,PositionEndDate1,PositionNo1,PositionDate1,PositionRemark1,IsDoublePosition,Position2,PositionLevel2,JobTitle2,CheckRemark,PositionStartDate2,PositionEndDate2,PositionNo2,PositionDate2,PositionRemark2,Class,ClassDate,IsLeave,LeaveReasion,LeaveDay,UserID,LeaveTo,LeaveNo,LeaveDate,LeaveSummary,LeaveRemark,AddUser,AddDate,UpdateUser,UpdateDate,Backup1,UserName,Backup2,Backup3,Backup4,Backup5,Backup6,Backup7,Sex");
            builder.Append(") values (");
            builder.Append("@Birthday,@ID,@Birthplace,@Nation,@Politics,@JoinPartDate,@GraduationDate,@GraduationSchoole,@HighestEducation,@Degree,@LogType,@Major,@JoinWorkDate,@Photo,@IsOfficer,@Remark,@RegistDay,@RegistWay,@RegistFrom,@RegistTo,@OrganID,@RegistNo,@CheckState,@RegistDate,@RegistSummary,@RegistRemark,@RegistState,@RegistType,@UserType,@IsHeighWith,@JobPositionType,@JobPositionName1,@JobPositionDate1,@CheckAdminID,@JobPositionName2,@JobPositionDate2,@JobPositionNo,@JobPositionDate,@JobPositionSummary,@JobPositionRemark,@AdminPosition,@EvaluationDate,@Position1,@PositionLevel1,@CheckDate,@JobTitle1,@PositionStartDate1,@PositionEndDate1,@PositionNo1,@PositionDate1,@PositionRemark1,@IsDoublePosition,@Position2,@PositionLevel2,@JobTitle2,@CheckRemark,@PositionStartDate2,@PositionEndDate2,@PositionNo2,@PositionDate2,@PositionRemark2,@Class,@ClassDate,@IsLeave,@LeaveReasion,@LeaveDay,@UserID,@LeaveTo,@LeaveNo,@LeaveDate,@LeaveSummary,@LeaveRemark,@AddUser,@AddDate,@UpdateUser,@UpdateDate,@Backup1,@UserName,@Backup2,@Backup3,@Backup4,@Backup5,@Backup6,@Backup7,@Sex");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] parameterArray = new SqlParameter[] { 
                new SqlParameter("@Birthday", model.Birthday), new SqlParameter("@ID", model.ID), new SqlParameter("@Birthplace", model.Birthplace), new SqlParameter("@Nation", model.Nation), new SqlParameter("@Politics", model.Politics), new SqlParameter("@JoinPartDate", model.JoinPartDate), new SqlParameter("@GraduationDate", model.GraduationDate), new SqlParameter("@GraduationSchoole", model.GraduationSchoole), new SqlParameter("@HighestEducation", model.HighestEducation), new SqlParameter("@Degree", model.Degree), new SqlParameter("@LogType", model.LogType), new SqlParameter("@Major", model.Major), new SqlParameter("@JoinWorkDate", model.JoinWorkDate), new SqlParameter("@Photo", model.Photo), new SqlParameter("@IsOfficer", model.IsOfficer), new SqlParameter("@Remark", model.Remark), 
                new SqlParameter("@RegistDay", model.RegistDay), new SqlParameter("@RegistWay", model.RegistWay), new SqlParameter("@RegistFrom", model.RegistFrom), new SqlParameter("@RegistTo", model.RegistTo), new SqlParameter("@OrganID", model.OrganID), new SqlParameter("@RegistNo", model.RegistNo), new SqlParameter("@CheckState", model.CheckState), new SqlParameter("@RegistDate", model.RegistDate), new SqlParameter("@RegistSummary", model.RegistSummary), new SqlParameter("@RegistRemark", model.RegistRemark), new SqlParameter("@RegistState", model.RegistState), new SqlParameter("@RegistType", model.RegistType), new SqlParameter("@UserType", model.UserType), new SqlParameter("@IsHeighWith", model.IsHeighWith), new SqlParameter("@JobPositionType", model.JobPositionType), new SqlParameter("@JobPositionName1", model.JobPositionName1), 
                new SqlParameter("@JobPositionDate1", model.JobPositionDate1), new SqlParameter("@CheckAdminID", model.CheckAdminID), new SqlParameter("@JobPositionName2", model.JobPositionName2), new SqlParameter("@JobPositionDate2", model.JobPositionDate2), new SqlParameter("@JobPositionNo", model.JobPositionNo), new SqlParameter("@JobPositionDate", model.JobPositionDate), new SqlParameter("@JobPositionSummary", model.JobPositionSummary), new SqlParameter("@JobPositionRemark", model.JobPositionRemark), new SqlParameter("@AdminPosition", model.AdminPosition), new SqlParameter("@EvaluationDate", model.EvaluationDate), new SqlParameter("@Position1", model.Position1), new SqlParameter("@PositionLevel1", model.PositionLevel1), new SqlParameter("@CheckDate", model.CheckDate), new SqlParameter("@JobTitle1", model.JobTitle1), new SqlParameter("@PositionStartDate1", model.PositionStartDate1), new SqlParameter("@PositionEndDate1", model.PositionEndDate1), 
                new SqlParameter("@PositionNo1", model.PositionNo1), new SqlParameter("@PositionDate1", model.PositionDate1), new SqlParameter("@PositionRemark1", model.PositionRemark1), new SqlParameter("@IsDoublePosition", model.IsDoublePosition), new SqlParameter("@Position2", model.Position2), new SqlParameter("@PositionLevel2", model.PositionLevel2), new SqlParameter("@JobTitle2", model.JobTitle2), new SqlParameter("@CheckRemark", model.CheckRemark), new SqlParameter("@PositionStartDate2", model.PositionStartDate2), new SqlParameter("@PositionEndDate2", model.PositionEndDate2), new SqlParameter("@PositionNo2", model.PositionNo2), new SqlParameter("@PositionDate2", model.PositionDate2), new SqlParameter("@PositionRemark2", model.PositionRemark2), new SqlParameter("@Class", model.Class), new SqlParameter("@ClassDate", model.ClassDate), new SqlParameter("@IsLeave", model.IsLeave), 
                new SqlParameter("@LeaveReasion", model.LeaveReasion), new SqlParameter("@LeaveDay", model.LeaveDay), new SqlParameter("@UserID", userID), new SqlParameter("@LeaveTo", model.LeaveTo), new SqlParameter("@LeaveNo", model.LeaveNo), new SqlParameter("@LeaveDate", model.LeaveDate), new SqlParameter("@LeaveSummary", model.LeaveSummary), new SqlParameter("@LeaveRemark", model.LeaveRemark), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@UpdateDate", model.UpdateDate), new SqlParameter("@Backup1", model.Backup1), new SqlParameter("@UserName", model.UserName), new SqlParameter("@Backup2", model.Backup2), new SqlParameter("@Backup3", model.Backup3), 
                new SqlParameter("@Backup4", model.Backup4), new SqlParameter("@Backup5", model.Backup5), new SqlParameter("@Backup6", model.Backup6), new SqlParameter("@Backup7", model.Backup7), new SqlParameter("@Sex", model.Sex)
             };
            return new CommandInfo { CommandText = builder.ToString(), Parameters = parameterArray };
        }

        public bool Import(int organID, OrganExportAndImport importData)
        {
            bool flag2;
            using (SqlConnection connection = new SqlConnection(Config.ConnectionString))
            {
                connection.Open();
                SqlTransaction trans = connection.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                try
                {
                    List<Organ> organList = importData.OrganList.FindAll(f => f.ParentID == 0);
                    if (this.ReturnImport(organID, organList, importData, connection, trans, cmd))
                    {
                        trans.Commit();
                        return true;
                    }
                    trans.Rollback();
                    return false;
                }
                catch (SqlException exception)
                {
                    trans.Rollback();
                    Log.Write(exception.InnerException.Message.ToString());
                    return false;
                }
                finally
                {
                    if (cmd != null)
                    {
                        cmd.Dispose();
                    }
                }
            }
            return flag2;
        }

        public bool ReturnImport(int organID, List<Organ> organList, OrganExportAndImport importData, SqlConnection connection, SqlTransaction trans, SqlCommand cmd)
        {
            using (List<Organ>.Enumerator enumerator = organList.GetEnumerator())
            {
                Predicate<OrganChangeLog> match = null;
                Predicate<User> predicate3 = null;
                Predicate<Organ> predicate4 = null;
                Organ organ;
                while (enumerator.MoveNext())
                {
                    organ = enumerator.Current;
                    CommandInfo organCommandInfo = this.GetOrganCommandInfo(organID, organ);
                    DBHelper.PrepareCommand(cmd, connection, trans, organCommandInfo.CommandText, (SqlParameter[]) organCommandInfo.Parameters);
                    object objA = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value))
                    {
                        trans.Rollback();
                        return false;
                    }
                    int nums = Convert.ToInt32(objA);
                    if (match == null)
                    {
                        match = f => f.OrganID == organ.OrganID;
                    }
                    foreach (OrganChangeLog log in importData.OrganChangeLogList.FindAll(match))
                    {
                        CommandInfo organLogCommandInfo = this.GetOrganLogCommandInfo(nums, log);
                        DBHelper.PrepareCommand(cmd, connection, trans, organLogCommandInfo.CommandText, (SqlParameter[]) organLogCommandInfo.Parameters);
                        object obj3 = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        if (object.Equals(obj3, null) || object.Equals(obj3, DBNull.Value))
                        {
                            trans.Rollback();
                            return false;
                        }
                    }
                    if (predicate3 == null)
                    {
                        predicate3 = delegate (User f) {
                            int? organizationID = f.OrganID;
                            int num = organ.OrganID;
                            return (organizationID.GetValueOrDefault() == num) && organizationID.HasValue;
                        };
                    }
                    using (List<User>.Enumerator enumerator3 = importData.UserList.FindAll(predicate3).GetEnumerator())
                    {
                        Predicate<UserChangeLog> predicate = null;
                        User user;
                        while (enumerator3.MoveNext())
                        {
                            user = enumerator3.Current;
                            DBHelper.PrepareCommand(cmd, connection, trans, "SELECT * FROM [USER] WHERE ID=" + user.ID, null);
                            bool flag = false;
                            using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                            {
                                DataSet dataSet = new DataSet();
                                adapter.Fill(dataSet, "ds");
                                cmd.Parameters.Clear();
                                flag = dataSet.Tables[0].Rows.Count > 0;
                            }
                            if (!flag)
                            {
                                CommandInfo info3 = this.GeUserCommandInfo(nums, user);
                                DBHelper.PrepareCommand(cmd, connection, trans, info3.CommandText, (SqlParameter[]) info3.Parameters);
                                object obj4 = cmd.ExecuteScalar();
                                cmd.Parameters.Clear();
                                if (object.Equals(obj4, null) || object.Equals(obj4, DBNull.Value))
                                {
                                    trans.Rollback();
                                    return false;
                                }
                                int userID = Convert.ToInt32(obj4);
                                if (predicate == null)
                                {
                                    predicate = f => f.UserID == user.UserID;
                                }
                                foreach (UserChangeLog log2 in importData.UserChangeLogList.FindAll(predicate))
                                {
                                    CommandInfo info4 = this.GeUserLogCommandInfo(userID, log2);
                                    DBHelper.PrepareCommand(cmd, connection, trans, info4.CommandText, (SqlParameter[]) info4.Parameters);
                                    object obj5 = cmd.ExecuteScalar();
                                    cmd.Parameters.Clear();
                                    if (object.Equals(obj5, null) || object.Equals(obj5, DBNull.Value))
                                    {
                                        trans.Rollback();
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                    if (predicate4 == null)
                    {
                        predicate4 = delegate(Organ f)
                        {
                            int? parentID = f.ParentID;
                            int organizationID = organ.OrganID;
                            return (parentID.GetValueOrDefault() == organizationID) && parentID.HasValue;
                        };
                    }
                    List<Organ> list = importData.OrganList.FindAll(predicate4);
                    if (list.Count > 0)
                    {
                        return this.ReturnImport(nums, list, importData, connection, trans, cmd);
                    }
                }
            }
            return true;
        }

        public bool UserDataImport(int organID, OrganExportAndImport importData)
        {
            bool flag3;
            using (SqlConnection connection = new SqlConnection(Config.ConnectionString))
            {
                connection.Open();
                SqlTransaction trans = connection.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                try
                {
                    bool flag = true;
                    foreach (User user in importData.UserList)
                    {
                        DBHelper.PrepareCommand(cmd, connection, trans, "SELECT * FROM [USER] WHERE ID='" + user.ID + "'", null);
                        bool flag2 = false;
                        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                        {
                            DataSet dataSet = new DataSet();
                            adapter.Fill(dataSet, "ds");
                            cmd.Parameters.Clear();
                            flag2 = dataSet.Tables[0].Rows.Count > 0;
                        }
                        if (!flag2)
                        {
                            CommandInfo info = this.GeUserCommandInfo(organID, user);
                            DBHelper.PrepareCommand(cmd, connection, trans, info.CommandText, (SqlParameter[]) info.Parameters);
                            object objA = cmd.ExecuteScalar();
                            cmd.Parameters.Clear();
                            if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value))
                            {
                                trans.Rollback();
                                flag = false;
                            }
                        }
                    }
                    if (flag)
                    {
                        trans.Commit();
                        return true;
                    }
                    trans.Rollback();
                    return false;
                }
                catch (SqlException exception)
                {
                    trans.Rollback();
                    Log.Write(exception.InnerException.Message.ToString());
                    return false;
                }
                finally
                {
                    if (cmd != null)
                    {
                        cmd.Dispose();
                    }
                }
            }
            return flag3;
        }
    }
}

